import android.graphics.Color
import com.google.gson.Gson
import com.yalemang.framework.cache.AppKeyValueImpl
import com.yalemang.photovalut.base.user.Skin
import com.yalemang.photovalut.base.user.User


class UserManager private constructor(){

    var user = User()

    companion object{
        const val KEY_USER = "KEY_USER"

        val instance: UserManager by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED){
            UserManager()
        }
    }

    fun init(){
        val jsonUser = AppKeyValueImpl.getByNoNull(KEY_USER, "")
        if (jsonUser.isNotEmpty()){
            user = Gson().fromJson(jsonUser, User::class.java)
        }
    }

    fun updatePassword(newPassword: String){
        user.password = newPassword
        saveUser()
    }

    fun updateSkin(newSkin: Skin){
        user.skin = newSkin
        saveUser()
    }

    private fun saveUser(){
        AppKeyValueImpl.save(KEY_USER,Gson().toJson(user))
    }

    fun getSkinData(): ArrayList<Skin> {
        val skinList = ArrayList<Skin>()
        skinList.add(Skin())
        skinList.add(Skin(Color.RED, Color.parseColor("#3D3D3D")))
        skinList.add(Skin(Color.parseColor("#836FFF"),Color.parseColor("#000000")))
        skinList.add(Skin(Color.parseColor("#7a378b"),Color.parseColor("#ff00ff")))
        skinList.add(Skin(Color.parseColor("#7a378b"),Color.parseColor("#ee7942")))
        skinList.add(Skin(Color.parseColor("#7a378b"),Color.parseColor("#b22222")))
        skinList.add(Skin(Color.parseColor("#7a378b"),Color.parseColor("#cd853f")))
        skinList.add(Skin(Color.parseColor("#7a378b"),Color.parseColor("#0099cc")))
        skinList.add(Skin(Color.parseColor("#BA55D3"),Color.parseColor("#CD9B1D")))

        return skinList
    }
}